﻿using System;
using System.Windows.Forms;
using phhui;
using System.IO;
using System.Collections;

namespace PQBlog
{
    public partial class AddDiary : Form
    {
        public AddDiary()
        {
            InitializeComponent();
        }

        private void AddDiary_Load(object sender, EventArgs e)
        {

        }

        private void btn_post_Click(object sender, EventArgs e)
        {
            tb_tag.Text = tb_tag.Text.Replace("，",",");
            tb_tag.Text = tb_tag.Text.Replace(" ", ",");
            if (!Directory.Exists("db/article")) Directory.CreateDirectory("db/article");
            FileMgr.createDirectoryByYYYYMMDD("db/article");
            int i = new Random().Next();
            string name = DateTime.Now.ToString("yyyyMMddhhmmss")+i.ToString();
            string fn = "db/article/" + DateTime.Now.ToString("yyyy/MM/dd/") + name + ".xml";
            if(!XmlHelper.self.CreateXmlDocument(fn, "data"))
            {
                MessageBox.Show("文件创建失败！");
                return;
            }
            Hashtable ht = new Hashtable();
            ht.Add("date", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            ht.Add("content", tb_content.Text);
            ht.Add("tag", tb_tag.Text);
            ht.Add("title", tb_title.Text);
            XmlHelper.self.InsertNode(fn, "data", false, "data", null, ht);

            Hashtable dir = new Hashtable();
            dir.Add("name", tb_title.Text);
            dir.Add("date", DateTime.Now.ToString("yyyy/MM/dd"));
            dir.Add("fn", name);
            dir.Add("tag", tb_tag.Text);
            XmlHelper.self.InsertNode("db/article/dir.xml","item",true,"data",dir,null);

            MessageBox.Show("添加成功！");
            this.Hide();
            WinMgr.self.showDiaryView();
        }
        private void AddDiary_FormClosed(object sender, FormClosedEventArgs e)
        {
            WinMgr.self.showMain();
        }
    }
}
